﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.Controls;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using HYRevitCode.FormUtility.ModelessFormControlSuite.ViewLayer;

namespace AdjustInstallationHeight
{
	public partial class BranchForm : BaseNoneSwitchForm
	{
		public List<BuiltInCategory> LstBuiltInCategory
		{
			get
			{
				return this.m_lstBuiltInCategory;
			}
			private set
			{
				this.m_lstBuiltInCategory = value;
			}
		}

		public List<FormStructure> LstFormData
		{
			get
			{
				return this.m_lstFormData;
			}
			set
			{
				this.m_lstFormData = value;
			}
		}

		public BranchForm(string strFileName, UIDocument inputDoc, IWin32Window input) : base(inputDoc, input)
		{
			this.m_FileName = strFileName;
			this.InitializeComponent();
		}

		private void ReplaceText(string strBranchName)
		{
            foreach (System.Windows.Forms.Control control in new List<System.Windows.Forms.Control>
			{
				this,
				this.lblToBeam,
				this.lblToFloor
			})
			{
				control.Text = Regex.Replace(control.Text, "电气", strBranchName);
			}
		}

		private void txtToBeam_TextChanged(object sender, EventArgs e)
		{
			if (!GroupCalculation.JudgeIsNaturalNumber(this.txtToBeam.Text))
			{
				YJKMessageBox.Show(this.m_FileName + "距离梁底距离取值范围为自然数");
				this.txtToBeam.Text = this.ReturnNumber(this.m_FileName, "梁").ToString();
				this.txtToBeam.Focus();
				this.txtToBeam.SelectAll();
			}
		}

		private void txtToFloor_TextChanged(object sender, EventArgs e)
		{
			if (!GroupCalculation.JudgeIsNaturalNumber(this.txtToFloor.Text))
			{
				YJKMessageBox.Show(this.m_FileName + "距离板底距离取值范围为自然数");
				this.txtToFloor.Text = this.ReturnNumber(this.m_FileName, "地板").ToString();
				this.txtToFloor.Focus();
				this.txtToFloor.SelectAll();
			}
		}

		private void BranchForm_Load(object sender, EventArgs e)
		{
			base.Icon = Resource.YArchitechIcon;
			this.ReplaceText(this.m_FileName);
			if (!this.ReadDlgData())
			{
				this.txtToBeam.Text = this.ReturnNumber(this.m_FileName, "梁").ToString();
				this.txtToFloor.Text = this.ReturnNumber(this.m_FileName, "地板").ToString();
			}
			this.RefreshFormData();
			base.SendContinueMessage();
		}

		private void BranchForm_FormClosed(object sender, FormClosedEventArgs e)
		{
			this.SaveDlgData();
		}

		private void GetLstBuiltInCategory(List<FormStructure> inputlstFormData)
		{
			this.m_lstBuiltInCategory.Clear();
			foreach (FormStructure formStructure in inputlstFormData)
			{
				this.m_lstBuiltInCategory.Add(formStructure.ElementType);
			}
		}

		private void SaveDlgData()
		{
			try
			{
				FileStream fileStream = new FileStream(Path.GetTempPath() + "AdjustInstallationHeightBranch.xml", FileMode.Create);
				StreamWriter streamWriter = new StreamWriter(fileStream);
				streamWriter.WriteLine(this.Text);
				streamWriter.WriteLine(this.txtToBeam.Text);
				streamWriter.WriteLine(this.txtToFloor.Text);
				if ("桥架高度调整" == this.Text)
				{
					streamWriter.WriteLine("桥架高度调整对话框位置坐标");
					streamWriter.WriteLine(base.Location.X);
					streamWriter.WriteLine(base.Location.Y);
				}
				streamWriter.Flush();
				streamWriter.Close();
				fileStream.Close();
			}
			catch (Exception)
			{
			}
		}

		private bool ReadDlgData()
		{
			string path = Path.GetTempPath() + "AdjustInstallationHeightBranch.xml";
			if (!File.Exists(path))
			{
				return false;
			}
			base.StartPosition = FormStartPosition.Manual;
			FileStream fileStream = new FileStream(path, FileMode.Open);
			StreamReader streamReader = new StreamReader(fileStream);
			streamReader.BaseStream.Seek(0L, SeekOrigin.Begin);
			if (this.Text == streamReader.ReadLine())
			{
				this.txtToBeam.Text = streamReader.ReadLine();
				this.txtToFloor.Text = streamReader.ReadLine();
			}
			if (this.txtToBeam.Text == "" || this.txtToFloor.Text == "")
			{
				return false;
			}
			try
			{
				if ("桥架高度调整对话框位置坐标" == streamReader.ReadLine())
				{
					int x = Convert.ToInt32(streamReader.ReadLine());
					int y = Convert.ToInt32(streamReader.ReadLine());
                    System.Drawing.Point location = new System.Drawing.Point(x, y);
					base.Location = location;
				}
			}
			catch
			{
			}
			streamReader.Close();
			fileStream.Close();
			return true;
		}

		private void RefreshFormData()
		{
			this.LstFormData.Clear();
			if (this.txtToBeam.Text != string.Empty && this.txtToFloor.Text != string.Empty)
			{
				double dDistanceToBeam = Convert.ToDouble(this.txtToBeam.Text);
				double dDistanceToFloor = Convert.ToDouble(this.txtToFloor.Text);
				if (this.Text.Contains("桥架"))
				{
					this.LstFormData.Add(new FormStructure(dDistanceToBeam, dDistanceToFloor, BuiltInCategory.OST_CableTray));
				}
				else if (this.Text.Contains("水管"))
				{
					this.LstFormData.Add(new FormStructure(dDistanceToBeam, dDistanceToFloor, BuiltInCategory.OST_PipeCurves));
				}
				else if (this.Text.Contains("风管"))
				{
					this.LstFormData.Add(new FormStructure(dDistanceToBeam, dDistanceToFloor, BuiltInCategory.OST_DuctCurves));
				}
			}
			this.GetLstBuiltInCategory(this.LstFormData);
		}

		private void BranchForm_MouseLeave(object sender, EventArgs e)
		{
			this.RefreshFormData();
			base.SendContinueMessage();
		}

		private int ReturnNumber(string strFileName, string strType)
		{
			int result = 0;
			if (!(strFileName == "桥架"))
			{
				if (!(strFileName == "水管"))
				{
					if (strFileName == "风管")
					{
						result = 100;
					}
				}
				else if (strType.Contains("梁"))
				{
					result = 100;
				}
				else
				{
					result = 350;
				}
			}
			else
			{
				result = 300;
			}
			return result;
		}

		private List<FormStructure> m_lstFormData = new List<FormStructure>();

		private List<BuiltInCategory> m_lstBuiltInCategory = new List<BuiltInCategory>();

		private string m_FileName = string.Empty;
	}
}
